import express from 'express'
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import ejs from 'ejs';
// import bodyParser from 'body-parser';


const __dirname = dirname(fileURLToPath(import.meta.url));

const app = express()
const PORT = 8888

// 静态文件
app.use(express.static(join(__dirname, 'public')));
app.set('views', join(__dirname, 'views'));
app.set('view engine', 'html');
app.engine('html', ejs.renderFile);

app.use(express.urlencoded({ extended: false }))
// 返回处理函数封装
app.use((req, res, next) => {
	// status: 0 为成功， 1 为失败
	res.cc = (err, status = 1) => {
		res.send({
			status,
			message: err instanceof Error ? err.message : err
		});
	};
	next();
});

import RenderRouter from './routes/render.js';
app.use(RenderRouter);

import AuthRouter from './routes/auth.js';
app.use(AuthRouter)

app.listen(PORT, () => console.log('Server running at http://localhost:' + PORT))